/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：ArenaMenuScreen.java <br>
 * 创建时间：2015年4月30日 上午11:39:00 <br>
 * 所属项目：GmudEX <br>
 * @author 12548
 */
package lostland.gmud.exv2.battle.netbattle;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import lostland.general.network.GmudHttp;
import lostland.general.network.GmudRequestListener;
import lostland.general.network.GmudRestUrl;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.battle.BattleScreen;
import lostland.gmud.exv2.battle.proc.holder.NetBattle;
import lostland.gmud.exv2.community.GmudUser;
import lostland.gmud.exv2.data.MainChar;
import lostland.gmud.exv2.data.Npc;
import lostland.gmud.exv2.data.Room;
import lostland.gmud.exv2.expand.GeneralMenuScreen;
import lostland.gmud.exv2.ui.NotificationScreen;

/**
 * 类名：ArenaMenuScreen <br>
 * 说明：
 *
 * @author 12548
 */
public class ArenaMenuScreen extends GeneralMenuScreen {

	BattleInfo[] bis;

	public ArenaMenuScreen(BattleInfo[] bis) {
		super(gets(bis), "请选择你要登上的擂台：");
		this.bis = bis;
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ScrollableMenuScreen#onCancel()
	 */
	@Override
	public void onCancel() {
		removeFromGame();
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ScrollableMenuScreen#onClick(int)
	 */
	@Override
	public void onClick(int i) {
		BattleInfo arena = bis[i];
		BattleScreen.getInstance().battleInfo = arena;

		if (arena.x >= 2) {
			new NotificationScreen("该擂台人数已满！").pushToGame();
		} else if (arena.x == 0) {
			//host

			if (!Gmud.mc.checkLimit(arena.limit)) {
				new NotificationScreen("你的人物太强大，不符合此擂台的限制！").pushToGame();
				return;
			}

			RequestParams rp = new RequestParams();

			rp.addBodyParameter("mc", new Gson().toJson(Gmud.mc));
			rp.addBodyParameter("svid", GmudUser.getInstance().getSaveObjectID());

			rp.addBodyParameter("arena", arena.getObjectId());

			new GmudHttp("正在创建主机……").postLocked(GmudRestUrl.Host, rp, new GmudRequestListener() {

				@Override
				public void onSucc(ResponseInfo<String> info) {
					JSONObject jo;
					try {
						jo = new JSONObject(info.result);
						String s = jo.getString(GmudHttp.KEY_STATE);

						if (s.equals(GmudHttp.STATE_ERROR)) {
							getGame().popToRootScreen();
							new NotificationScreen("服务器错误！！").pushToGame();
						} else if (s.equals(GmudHttp.STATE_OK)) {
							//FIXME
							BattleScreen.getInstance().idstamp = jo.getInt("idstamp");
							BattleScreen.getInstance().uuid = jo.getString("uuid");

							new WaitingForJoinScreen().pushToGame();
						} else {
							getGame().popToRootScreen();
							new NotificationScreen("发生了未知错误！！").pushToGame();
						}
					} catch (JSONException e) {
						if (Gmud.DEBUG) {
							e.printStackTrace();
						}

						getGame().popToRootScreen();
						new NotificationScreen("发生了未知错误！！").pushToGame();
					}
				}

				@Override
				public void onFail(HttpException error, String msg) {
					getGame().popToRootScreen();
					new NotificationScreen("网络连接失败！！！").pushToGame();
				}
			});


		} else {

			if (!Gmud.mc.checkLimit(arena.limit)) {
				new NotificationScreen("你的人物太强大，不符合此擂台的限制！").pushToGame();
				return;
			}

			RequestParams rp = new RequestParams();

			rp.addBodyParameter("mc", new Gson().toJson(Gmud.mc));
			rp.addBodyParameter("svid", GmudUser.getInstance().getSaveObjectID());

			rp.addBodyParameter("arena", arena.getObjectId());

			new GmudHttp("正在加入主机……").postLocked(GmudRestUrl.Join, rp, new GmudRequestListener() {

				@Override
				public void onSucc(ResponseInfo<String> info) {
					JSONObject jo;
					try {
						jo = new JSONObject(info.result);
						String s = jo.getString(GmudHttp.KEY_STATE);

						if (s.equals(GmudHttp.STATE_ERROR)) {
							getGame().popToRootScreen();
							new NotificationScreen("服务器错误！！").pushToGame();
						} else if (s.equals(GmudHttp.STATE_OK)) {
							//FIXME
							BattleScreen.getInstance().idstamp = jo.getInt("idstamp");
							BattleScreen.getInstance().uuid = jo.getString("uuid");

							Gson gson = new Gson();
							MainChar hostmc = gson.fromJson(jo.getJSONObject("host_mc").toString(), MainChar.class);

							final Npc npc = new Room().npc(hostmc);
							npc.refresh();
							int x = npc.look(-1, -1).id;

							getGame().popToRootScreen();
							BattleScreen.battle(-1, x, new NetBattle());
							BattleScreen.getInstance().networkMode = NetworkMode.CLIENT;
						} else {
							getGame().popToRootScreen();
							new NotificationScreen("发生了未知错误！！").pushToGame();
						}
					} catch (JSONException e) {
						if (Gmud.DEBUG) {
							e.printStackTrace();
						}

						getGame().popToRootScreen();
						new NotificationScreen("发生了未知错误！！").pushToGame();
					}
				}

				@Override
				public void onFail(HttpException error, String msg) {
					getGame().popToRootScreen();
					new NotificationScreen("网络连接失败！！！").pushToGame();
				}
			});


			//join
		}

	}

	public static ArrayList<String> gets(BattleInfo[] bis) {
		ArrayList<String> as = new ArrayList<>();

		for (BattleInfo si : bis) {
			String s = si.name;
			if (si.x == 0) {
				s += "(0/2)";
			} else if (si.x == 1) {
				s += "(1/2)";
			} else {
				s += "(2/2)";
			}
			as.add(s);
		}

		return as;
	}

}
